//
//  DropTable.m
//  FaceRecognition
//
//  Created by zz on 16/12/7.
//  Copyright © 2016年 Sylar. All rights reserved.
//

#import "DropTable.h"

#define CellHeight 44

@interface DropTable() <UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>

@property (nonatomic,strong) UITableView *table;

@property (nonatomic,strong) UIView *bgView;

/*tableview下面的imageView用于显示 带箭头的背景图片**/
@property (nonatomic,strong) UIImageView *tableImageView;

/**外部点击调用下拉框按钮相对于下拉背景的frame*/
@property (nonatomic,assign) CGRect btnFrame;

@end

@implementation DropTable


#pragma mark -LifeCycle
- (instancetype)init{
    
    if (self = [super init]) {
        
        self.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGH);
        
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGH)];
        _bgView.backgroundColor = [UIColor blackColor];
        _bgView.alpha = 0.2;
        [self addSubview:_bgView];
        
        _tableWidth = _tableWidth > 0 ? _tableWidth : 120;      //默认宽度120
        _visibleCellCounts = _visibleCellCounts > 0 ? _visibleCellCounts : 4; //默认显示3个cell
        _maginRight = _maginRight > 0 ? _maginRight : 10;       //右边距默认是10
        _tableY = _tableY > 0 ? _tableY : SafeAreaTopHeight + 10;
        
        
        _table = [[UITableView alloc] initWithFrame:CGRectMake(150, 100,180, _tableWidth) style:UITableViewStylePlain];
        _table.delegate = self;
        _table.dataSource = self;
        _table.tableFooterView = [[UIView alloc] init];
        _table.separatorStyle = UITableViewCellSeparatorStyleNone;
        _table.showsVerticalScrollIndicator = NO;
        _table.backgroundColor = [UIColor clearColor];
        [self addSubview:_table];
        
        _tableImageView = [[UIImageView alloc] init];
        [self addSubview:_tableImageView];
        
    
        [[UIApplication sharedApplication].keyWindow addSubview:self];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [_bgView addGestureRecognizer:tap];
        tap.delegate = self;
        self.userInteractionEnabled = YES;

    }
    
    return self;
}

- (instancetype)initWithView:(UIView *)view{
    
    if (self = [self init]) {
        
        _btnFrame = [view.superview convertRect:view.frame toView:nil];
        
    }
    return self;
}

- (void)dealloc{
    
    NSLog(@"view dealloc");
}


- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    
    CGFloat tableW = _tableWidth;
    CGFloat tableH = _lists.count > 4 ? CellHeight * _visibleCellCounts: CellHeight * _lists.count;
    CGFloat tableX = SCREENWIDTH - _maginRight - tableW;
    CGFloat tableY = _tableY;
    
    _table.frame = CGRectMake(tableX, tableY, tableW, tableH);
    
    CGFloat tableImgX,tableImgY,tableImgW,tableImgH;
    
    NSString *imageName;
    
    tableImgX = tableX - 2;
    tableImgY = tableY - 10;
    tableImgW = tableW + 4;
    tableImgH = tableH + 10;
    
    _tableImageView.frame = CGRectMake(tableImgX, tableImgY, tableImgW, tableImgH);
    
    [self bringSubviewToFront:_table];
    
    if (_localtion == localtionTypeMiddle) {
        
        //让tableview居中显示
        CGPoint center = _table.center;
        center.x = self.center.x;
        _table.center = center;
        
        _tableImageView.center = center;
        
        
        _tableImageView.y = tableY - 9.5;
        _tableImageView.height = tableH + 9.5;
        
        imageName = @"listdrop_radio_125onecenter_";
        
        
    }else if (_localtion == localtionTypeRight){
        
        imageName = @"listdrop_radio_four_";
        
    }else if (_localtion == localtionTypeCustom){ //自定义居中箭头显示
    
        NSAssert(_btnFrame.size.width > 0, @"btnframe must't be nil in the condition of localtionTypeCustom");
        
        CGFloat maxY = CGRectGetMaxY(_btnFrame);
        CGFloat centerX = CGRectGetMidX(_btnFrame);
        
        
        CGPoint center = _table.center;
        center.x = centerX;
        _table.center = center;
        
        _tableImageView.center = center;
        _table.y = maxY + 10;
        _tableImageView.y = maxY;
        imageName = @"listdrop_radio_125onecenter_";
    }else if (_localtion == localtionTypeCustomRight){ //自定义靠右箭头显示
        
        NSAssert(_btnFrame.size.width > 0, @"btnframe must't be nil in the condition of localtionTypeCustom");
        
        
        CGFloat maxY = CGRectGetMaxY(_btnFrame);
        CGFloat centerX = CGRectGetMidX(_btnFrame);
        centerX -= _maginRight;
        
        CGPoint center = _table.center;
        center.x = centerX;
        _table.center = center;

        _tableImageView.center = center;
        _table.y = maxY + 10;
        _tableImageView.y = maxY;
        imageName = @"listdrop_radio_four_";
    }else {
        imageName = @"listdrop_radio_125onecenter_";
    }
    
    UIImage *image = [UIImage imageNamed:imageName];
    image = [image stretchableImageWithLeftCapWidth:0 topCapHeight:16];
    _tableImageView.image = image;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _lists.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *textStr = _lists[indexPath.row];
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _tableWidth, cell.contentView.frame.size.height)];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor colorWithHexString:@"#39adff"];
    label.text = textStr;
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 43.5, _tableWidth, 0.5)];
    lineView.backgroundColor = [UIColor colorWithHexString:@"#DEDEDE"];
    [cell.contentView addSubview:lineView];
    
    [cell.contentView addSubview:label];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([_delegate respondsToSelector:@selector(tableViewCellSelected:)]) {
        
        [_delegate tableViewCellSelected:indexPath.row];
    }
    
    [self removeFromSuperview];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    
    return YES;
}

- (void)tapAction:(UIGestureRecognizer *)gezr{
    
    [self removeFromSuperview];
}

#pragma mark -Setter
- (void)setLists:(NSArray *)lists{
    
    _lists = lists;
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
    [_table reloadData];
}

- (void)setLocaltion:(localtionType)localtion{
    
    _localtion = localtion;
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
}

- (void)setTableWidth:(CGFloat)tableWidth{
    
    _tableWidth = tableWidth;
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
}


@end
